$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'mercharReport/storemember/list',
        datatype: "json",
        colModel: [
            {label: '公司名称', name: 'name', index: 'sid', width: 50},
            {label: '预存款', name: 'advance', index: 'advance', width: 80},
            {label: '门店会员类型', name: 'merchantsType', index: 'merchants_type', width: 80},
            {label: '门店业务员', name: 'username', index: 'username', width: 80},
            {label: '地址', name: 'addr', index: 'addr', width: 80},
            {label: '手机', name: 'mobile', index: 'mobile', width: 80},
            {label: '固定电话', name: 'tel', index: 'tel', width: 80},
            {label: '挂账客户', name: 'hangsAccountCustomer', index: 'hangs_account_customer', width: 80},
            {label: '价格账套', name: 'favouredName', index: 'favoured_name', width: 80},
            {label: '联系人', name: 'nickname', index: 'nickname', width: 80}
        ],
        viewrecords: true,
        height: window.innerHeight-125,
        rowNum: 25,
        rowList: [25, 50, 80],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount",
            userdata: "userData"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        storeMember: {},
        q: {
            mobile: '',
            name: '',
            beginAdvance: null,
            endAdvance: null,
            hangsAccountCustomer: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.storeMember = {};
        },
        update: function (event) {
            var sid = getSelectedRow();
            if (sid == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(sid)
        },
        saveOrUpdate: function (event) {
            var url = vm.storeMember.sid == null ? "mercharReport/storemember/save" : "mercharReport/storemember/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.storeMember),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var sids = getSelectedRows();
            if (sids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "mercharReport/storemember/delete",
                    contentType: "application/json",
                    data: JSON.stringify(sids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (sid) {
            $.get(baseURL + "mercharReport/storemember/info/" + sid, function (r) {
                vm.storeMember = r.storeMember;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {//postData此数组内容直接赋值到url上，参数类型：{name1:value1…}
                    'mobile': vm.q.mobile.trim(),
                    'name': vm.q.name.trim(),
                    'beginAdvance': vm.q.beginAdvance,
                    'endAdvance': vm.q.endAdvance,
                    'hangsAccountCustomer': vm.q.hangsAccountCustomer
                },
                page: 1
            }).trigger("reloadGrid");
        },
        reset() {
            vm.q.mobile = '';
            vm.q.name = '';
            vm.q.beginAdvance = '';
            vm.q.endAdvance = '';
            vm.q.hangsAccountCustomer = '';
            vm.reload();
        },
        exportExcel() {
            //导出excel
            vm.q.mobile = vm.q.mobile.trim();
            vm.q.name = vm.q.name.trim();
            vm.q.beginAdvance = vm.q.beginAdvance.trim();
            vm.q.beginAdvance = vm.q.beginAdvance.trim();
            vm.q.endAdvance = vm.q.endAdvance.trim();
            vm.q.name = vm.q.name.trim();
            const data = encodeURIComponent(JSON.stringify(vm.q));
            window.location.href = baseURL + "mercharReport/storemember/exportExcel?postData=" + data;
        },
        objTrim(obj) {
            Object.keys(obj).forEach(k => {
                obj[k] = obj[k].trim()
            })
        }
    }
});